<?php
/**
 * Created by PhpStorm.
 * User: chenyeubin
 * Date: 2020\2\21 0021
 * Time: 10:12
 */

namespace api\modules\v1\controllers\work;

use common\models\base\Profession;
use Yii;
use api\controllers\OnAuthController;
use yii\web\HttpException;

class ReportController extends OnAuthController
{
    /**
     * @var string
     */
    public $modelClass = '';

    /**
     * TODO 作废
     * 获取我的报表中上面的总览概述
     * 'count_patient' => 总患者,
     * 'today_patient_add' => 今日新增患者,
     * 'count_unfollow' => 总过期未回访数
     * 'count_unfollow_week' => 本周过期未回访数,
     * 'count_outpatient_type' => 总复诊人数,
     * 'count_outpatient_type_week' => 本周总复诊人数
     * 'count_follow' => 总已回访次数
     * 'month_follow_add' => 本月已回访次数
     * @return array
     */
    public function actionOverView()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = $this->projectId;
        $attr = Yii::$app->services->attr->findInfo([
            'member_id' => Yii::$app->user->identity->member_id,
            'project_id' => $this->projectId,
        ]);
        // 是否返回客服就诊类型
        if ($attr && $attr->profession_id == Profession::ID_CUSTOMER) { // 客服
            $params['customer'] = 1;
            $profession_id = 302;
        } else {
            $params['customer'] = 2;
            $profession_id = 301;
        }
        // 今日新增患者数、总患者数、总过期未回访数、本周过期未回访数、总复诊人数、本周总复诊人数
        $patientNum = Yii::$app->services->patientUser->patientUser($params);

        // 已回访次数、总已回访次数
        $followNum = Yii::$app->services->patientCallback->visitsNum($params);

        $res = $patientNum;
        $res['count_follow'] = $followNum['count_follow'];
        $res['month_follow_add'] = $followNum['month_follow_add'];
        $res['month_follow_add_stime'] = $followNum['month_follow_add_stime'];
        $res['month_follow_add_etime'] = $followNum['month_follow_add_etime'];
        $res['profession_id'] = $profession_id;
        $res['outpatient_type_id'] = Yii::$app->services->statisticsManagementReport::OUTPATIENT_TYPE['referral'];

        return $res;
    }

    /**
     * @api {get} /v1/work/report/my 我的报表
     * @apiName my
     * @apiGroup WorkReport
     *
     * @apiParam {Number} add_type                      新增患者类型【1今日、2昨天(-1天)、3周(-7天)、4月(-30天)】
     * @apiParam {Number} arrive_type                   已到类型【1今日、2昨天、3周(-7天)、4月(-30天)】
     * @apiParam {Number} order_arrive_type             预到类型【1今日、2明天(+1天)、3周(+7天)、4月(+30天)】（未来日期）
     * @apiParam {Number} unarrive_type                 过期未到类型【2昨天、3周(-7天)、4月(-30天)】
     * @apiParam {Number} callback_type                 已回访次数类型【1今日、2昨天、3周(-7天)、4月(-30天)】
     * @apiParam {Number} uncallback_type               过期未回访类型【2昨天、3周(-7天)、4月(-30天)】
     * @apiParam {Number} page                          页码
     * @apiParam {Number} pageSize                      每页展示数
     *
     * @apiSuccess {Object}  data                                          返回数据
     * @apiSuccess {Number}  data.page                                     当前页码
     * @apiSuccess {Number}  data.pageSize                                 每页显示数
     * @apiSuccess {Number}  data.total                                    总数
     * @apiSuccess {Array}   data.list                                     患者列表
     * @apiSuccess {Number}  data.list.id                                  患者ID
     */
    public function actionMy()
    {
        $params = Yii::$app->request->get();
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['project_id'] = $this->projectId;
        $params['add_type'] = intval($params['add_type'] ?? 1);
        $params['arrive_type'] = intval($params['arrive_type'] ?? 1);
        $params['order_arrive_type'] = intval($params['order_arrive_type'] ?? 1);
        $params['unarrive_type'] = intval($params['unarrive_type'] ?? 2);
        $params['callback_type'] = intval($params['callback_type'] ?? 1);
        $params['uncallback_type'] = intval($params['uncallback_type'] ?? 2);

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        // 是否返回客服就诊类型
        if (Yii::$app->params['is_customer'] == 1) { // 客服
            $params['belong_member_id'] = $params['member_id'];
            $params['is_customer'] = 1;
        } else {// 医助
            $params['belong_assistant_id'] = $params['member_id'];
            $params['is_customer'] = 0;
        }

        // 查询结果
        return Yii::$app->services->patientCallback->myReport($params, (int)$page, (int)$pageSize);
    }

    /**
     * 回访报表
     * start_time 开始日期
     * end_time 结束日期
     *
     *'time_list' => 时间段数组,
     * 'achieve_callback' => 已回访数据,
     * 'no_callback' => 未回访数据,
     * 'state_arr' => 回访状态数据,
     * 'state_list' => 回访状态数组,
     * @return array
     * @throws HttpException
     */
    public function actionCallbackReport()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = $this->projectId;
        // 回访曲线、回访状态
        $diffTime = (strtotime($params['end_time']) - strtotime($params['start_time'])) / 86400;
        if ($diffTime > 366) {
            throw new HttpException(422, '查询范围不能大于一年！');
        }
        $callbackReport = Yii::$app->services->patientCallback->callbackReport($params);

        return $callbackReport;
    }

    /**
     * 获取效果类型、就诊类型数据
     *
     * 'effect_type_list' => 效果类型名称数组,
     * 'effect_data' => 效果类型数据,
     * 'outpatient_type_list' => 就诊类型名称数组,
     * 'outpatient_data' => 就诊类型数据
     * @return array
     */
    public function actionPatientReport()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = $this->projectId;

        // 效果类型、就诊类型
        $patientReport = Yii::$app->services->patientUser->patientReport($params);

        return $patientReport;
    }
}
